#include "time.h"

void tim2Init()				//-------定时进入中断获取传感器数据获取
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef  NVIC_InitStruct;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	//基本定时器
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = 2000;//装载值，计数次数,一个计时周期为200ms
	TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;//预分频，基础频率为84MHz,分频后100um计数一次
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);//基本定时器初始化

	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);	//设置中断源
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;//设置中断通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//中断使能
	NVIC_Init(&NVIC_InitStruct);//定时器中断初始化
}

void tim6Init()				//----按键复用判断定时器
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef  NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
	//基本定时器
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = 10000;//装载值，计数次数,一个计时周期为10ms
	TIM_TimeBaseInitStruct.TIM_Prescaler = 84 -1;//预分频，基础频率为84MHz
	TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStruct);//基本定时器初始化
	
	TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);	//设置中断源
	NVIC_InitStruct.NVIC_IRQChannel = TIM6_DAC_IRQn;//设置中断通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//中断使能
	NVIC_Init(&NVIC_InitStruct);//定时器中断初始化
}

void tim3Init()				//----自定义延时函数定时器
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = 84-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
	
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
}


extern vu16 USART3_RX_STA;


//用作串口3数据断帧
void tim7Init(u16 arr,u16 psc)
{	
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);//TIM7时钟使能    
	
	//定时器TIM7初始化
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
 
	TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE ); //使能指定的TIM7中断,允许更新中断

	 	  
	NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;		//子优先级1
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
	
}

void myDelayUs(u16 nus)
{
	u32 cnt;
	cnt = nus;
	TIM_Cmd(TIM3, ENABLE);
	while(cnt--)
	{
		while(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) == RESET);
		TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	}
	TIM_Cmd(TIM3, DISABLE);
}

void myDelayMs(u16 nms)
{
	u32 cnt;
	cnt = nms*1000;
	TIM_Cmd(TIM3, ENABLE);
	while(cnt--)
	{
		while(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) == RESET);
		TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	}
	TIM_Cmd(TIM3, DISABLE);
}

//定时器7中断服务程序		    
void TIM7_IRQHandler(void)
{ 	
	if (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET)//是更新中断
	{	 			   
		USART3_RX_STA|=1<<15;	//标记接收完成
		TIM_ClearITPendingBit(TIM7, TIM_IT_Update  );  //清除TIM7更新中断标志    
		TIM_Cmd(TIM7, DISABLE);  //关闭TIM7 
	}	    
}
 


